/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-09
 * Time: 19:04
 */
public class MyLinkedList {

    //节点对象（静态内部类）
    static class ListNode {
        int val;
        ListNode prev;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    ListNode head;
    ListNode last;

    //头插法
    public void addFirst(int data){
        ListNode cur = new ListNode(data);
        if (this.head == null) {
            this.head = this.last = cur;
        }else {
            cur.next = this.head;
            this.head.prev = cur;
            this.head = cur;
        }
    }

    //尾插法
    public void addLast(int data){
        ListNode cur = new ListNode(data);
        if (this.head == null) {
            this.head = this.last = cur;
        }else {
            this.last.next = cur;
            cur.prev = this.last;
            this.last = cur;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        try {
            int len = size();
            isIllegal(index);
            if (index  == 0) {
                addFirst(data);
                return;
            }
            if (index == len) {
                addLast(data);
                return;
            }

            ListNode newNode = new ListNode(data);
            ListNode cur = this.head;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            newNode.next = cur;
            newNode.prev = cur.prev;
            cur.prev.next = newNode;
            cur.prev = newNode;
        }catch (SubscriptException e) {
            e.printStackTrace();
        }
    }
    //判断下标是否合法
    private void isIllegal(int index) {
        if (index < 0 || index > size()) {
            throw new SubscriptException("插入位置异常！");
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (this.head == null) {
            System.out.println("链表为空！");
            return;
        }
        ListNode cur = find(key);
        if (cur == null) {
            System.out.println("链表中没有该元素！");
            return;
        }
        //万一头节点是
        if (this.head == cur) {
            this.head = this.head.next;
            if (this.head != null) {
                this.head.prev = null;
            }else {
                this.last = null;
            }
            return;
        }
        //万一尾节点是
        if (this.last == cur) {
            this.last = this.last.prev;
            this.last.next = null;
            return;
        }

        cur.prev.next = cur.next;
        cur.next.prev = cur.prev;

    }
    //找到key第一次出现的位置
    private ListNode find(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (this.head == null) {
            System.out.println("链表为空！");
            return;
        }
        if (find(key) == null) {
            System.out.println("链表中无该节点！");
            return;
        }
        ListNode slow = this.head;
        ListNode fast = this.head.next;
        while (fast != null) {
            if (fast.val == key && fast == this.last) {
                this.last = this.last.prev;
                this.last.next = null;
                break;
            }
            if (fast.val == key) {
                slow.next = fast.next;
                fast.next.prev = slow;
            }else {
                slow = fast;
            }
            fast = fast.next;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            this.head.prev = null;
        }
    }

    //得到单链表的长度
    public int size(){
        int size = 0;
        ListNode cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public void display(){
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        this.head = null;
        this.last = null;
    }
    public void clear2() {
        ListNode cur = this.head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curN;
        }
        this.head = null;
        this.last = null;
    }
}
